$(function () {

    getBannerList();

    //获取推荐的精品课程
    getGroomDataList("IBC", "course");
    //获取免费直播列表
    getCourseOpenList();
    //获取推荐讲师
    getGroomDataList("IPT", "teacher");
    //获取推荐图书
    getGroomDataList("IRB", "book");
    //获取推荐资讯
    getGroomDataList("IPA", "article");

    $("body").on("click",".i-c-tab__wrap h4",function () {//点击切换精品课程、热门课程、推荐套餐
        $(this).addClass("active").siblings().removeClass("active");
        var placeKey = $(this).data('id');
        getGroomDataList(placeKey, "course");
    }).on('click', 'div[data-course]', function () {//查看课程详情
        var courseId = $(this).data('course');
        window.location = baseloaction + '/web/course/courseInfo?courseId=' + courseId;
    }).on('click', 'section[data-open]', function () {
        var openId = $(this).data('open');
        window.location.href = baseloaction + '/web/course/open/index?openId='+openId;
    });

    /**
     * 异步加载banner图
     */
    function getBannerList() {
        $.ajax({
            url: baseloaction + '/web/index/bannerList',
            type: 'post',
            dataType: 'text',
            success: function (result) {
                $("#banner").html(result);
                //幻灯片轮播效果
                $('.carousel').carousel();
            },
            error: function () {
                
            }
        })
    }

    /**
     * 获取推荐数据
     * @param placeKey
     * @param contextId
     */
    function getGroomDataList(placeKey, contextId) {
        $.ajax({
            url: baseloaction + '/web/index/queryGroomDataList',
            data: {"placeKey":placeKey},
            type: 'post',
            dataType: "text",
            success: function (result) {
                $("#"+ contextId).html(result);
                if("IBC"==placeKey || "IHC"==placeKey || "IRP"==placeKey || "IPA"==placeKey){
                    lazyCourseImage();
                }else if("IPT"==placeKey){
                    lazyTeacherImage();
                }else {
                    lazyBookImage();
                }

            },
            error: function (error) {
                var _html = '<section class="no-data__wrap"><span class="no-data-ico"></span><p class="mt20"><span class="c-666">还没有数据哦~~~</span></p></section>'
                $("#"+ contextId).html(_html);
            }
        })
    }

    /**
     * 获取免费直播列表
     */
    function getCourseOpenList() {
        $.ajax({
            url: baseloaction + '/web/index/findCourseOpenList',
            type: 'post',
            dataType: "text",
            success: function (result) {
                $("#courseOpen").html(result);
                lazyCourseImage();
                lazyStudentImage();
            },
            error: function (error) {
                var _html = '<section class="no-data__wrap"><span class="no-data-ico"></span><p class="mt20"><span class="c-666">还没有数据哦~~~</span></p></section>'
                $("#courseOpen").html(_html);
            }
        })
    }

    $("body").on('click','.openBanner',function(){
        if($(this).data("jump")==2){
            window.open($(this).data("soure"),"_blank")
        }else{
            window.location.href=$(this).data("soure");
        }
        //计算uv pv
        $.ajax({
            url: baseloaction + '/web/index/savePageView',
            type: 'post',
            dataType: "json",
        })

    });

});